package com.lx.huimail.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lx.huimail.R;
import com.lx.huimail.activity.p_status_act.WaitGetGoodsActivity;
import com.lx.huimail.configs.Config;
import com.lx.huimail.weight.MyListView;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderManagerAdapter1 extends RecyclerView.Adapter<OrderManagerAdapter1.MyViewHolder>{
    private Context mContext;
    private String array;
    ArrayList<HashMap<String, String>> listItem = new ArrayList<>();
    private DisplayMetrics dm;

    private PopAdapter madapter;
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public OrderManagerAdapter1(Context mContext, ArrayList<HashMap<String, String>> listItem){
        this.mContext=mContext;
        this.listItem=listItem;

        dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.gridlist6, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        array = listItem.get(position).get("array");
        holder.orderno.setText("订单号："+listItem.get(position).get("orderno"));
        holder.jine.setText(listItem.get(position).get("ordermoney")+"元"+"");
        holder.shuliang.setText("共"+listItem.get(position).get("ordercount")+"件商品");
        holder.xingming.setText(listItem.get(position).get("custname"));
        holder.address.setText(listItem.get(position).get("receiveraddr"));
        holder.sender.setText("配送员："+listItem.get(position).get("name")+"");
        try {
            List<Map<String, Object>> orderLists = new ArrayList<Map<String, Object>>();
            JSONArray jsonArray = new JSONArray(array);
            for (int i = 0; i < jsonArray.length(); i++) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("count", jsonArray.getJSONObject(i).getString("count"));
                map.put("money", jsonArray.getJSONObject(i).getString("money"));
                map.put("proname", jsonArray.getJSONObject(i).getString("proname"));
                map.put("sendtime", jsonArray.getJSONObject(i).getString("sendtime"));
                map.put("picname", jsonArray.getJSONObject(i).getString("picname"));
                orderLists.add(map);
            }
            madapter = new PopAdapter(mContext, orderLists);
            holder.lv.setAdapter(madapter);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (listItem.get(position).get("sendstatus").equals("0")){
            holder.zhuangtai.setText("等待配送");
            holder.lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int lvposition, long id) {
                    Intent intent=new Intent();
                    intent.putExtra("position",position);
                    intent.putExtra("status","0");
                    intent.putExtra("orderno",listItem.get(position).get("orderno"));
                    intent.putExtra("warnflag",listItem.get(position).get("warnflag"));
                    intent.putExtra("online1",listItem.get(position).get("online1"));
                    intent.setClass(mContext, WaitGetGoodsActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }else if(listItem.get(position).get("sendstatus").equals("1")){
            holder.zhuangtai.setText("正在配送");
            holder.lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int lvposition, long id) {
                    Intent intent=new Intent();
                    intent.putExtra("position",position);
                    intent.putExtra("status","1");
                    intent.putExtra("orderno",listItem.get(position).get("orderno"));
                    intent.putExtra("warnflag",listItem.get(position).get("warnflag"));
                    intent.putExtra("online1",listItem.get(position).get("online1"));
                    intent.setClass(mContext, WaitGetGoodsActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }else if(listItem.get(position).get("sendstatus").equals("2")){
            holder.zhuangtai.setText("已完成");
            holder.lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int lvposition, long id) {
                    Intent intent=new Intent();
                    intent.putExtra("position",position);
                    intent.putExtra("status","2");
                    intent.putExtra("orderno",listItem.get(position).get("orderno"));
                    intent.putExtra("warnflag",listItem.get(position).get("warnflag"));
                    intent.putExtra("online1",listItem.get(position).get("online1"));
                    intent.setClass(mContext, WaitGetGoodsActivity.class);
                    mContext.startActivity(intent);
                }
            });

        }

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
        }
    }

    @Override
    public int getItemCount()
    {
        return listItem.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {

        public TextView jine, shuliang, zhuangtai, xingming, address, sender, orderno;
        public LinearLayout relativeLayout;
        public MyListView lv;
        public MyViewHolder(View view)
        {
            super(view);
            jine = (TextView) view.findViewById(R.id.jine);
            orderno = (TextView) view.findViewById(R.id.orderno);
            shuliang = (TextView) view.findViewById(R.id.count);
            zhuangtai = (TextView) view.findViewById(R.id.sendstatus);
            xingming = (TextView) view.findViewById(R.id.xingming);
            address = (TextView) view.findViewById(R.id.address);
            sender = (TextView) view.findViewById(R.id.sender);
            lv = (MyListView) view.findViewById(R.id.listView);
        }
    }
    public class PopAdapter extends android.widget.BaseAdapter {
        List<Map<String, Object>> Lists;
        private Context context;
        int screenWidth;
        int screenHeight;

        public PopAdapter(Context context, List<Map<String, Object>> Lists) {
            super();
            this.context = context;
            this.Lists = Lists;
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels;
        }

        @Override
        public int getCount() {
            if (Lists.size() == 0) {
                return 0;
            } else {
                return Lists.size();
            }

        }

        @Override
        public Object getItem(int position) {
            return Lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.gridlist7, null);
                holder.logo = (ImageView) convertView.findViewById(R.id.logo);
                holder.mingcheng = (TextView) convertView.findViewById(R.id.mingcheng);
                holder.number = (TextView) convertView.findViewById(R.id.number);
                holder.jiage = (TextView) convertView.findViewById(R.id.jiage);
                holder.time = (TextView) convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            } else {
                // 取出holder
                holder = (ViewHolder) convertView.getTag();
            }
            ViewGroup.LayoutParams para = holder.logo.getLayoutParams();
            para.width = (screenWidth - dip2px(context, 55)) / 3;
            para.height = para.width * 3 / 4;//
            holder.logo.setLayoutParams(para);
            String uri = Config.url + "tmp/" + Lists.get(position).get("picname") + "";
            Picasso.with(context).load(uri)
                    .placeholder(R.drawable.detail_show_1)
                    .error(R.drawable.detail_show_1)
                    .into(holder.logo);
            Date date = new Date(Long.valueOf(Lists.get(position).get("sendtime").toString()));
            SimpleDateFormat format;
            format = new SimpleDateFormat("yyyy-MM-dd");
            holder.time.setText(format.format(date));
            holder.mingcheng.setText(Lists.get(position).get("proname").toString());
            if (Lists.get(position).get("count").toString().equals("null")) {
                holder.number.setText("数量：" + "0" + "");
            } else {
                holder.number.setText("数量：" + Lists.get(position).get("count")+"");
            }
            holder.jiage.setText("合计："+Lists.get(position).get("money")+"元");

            return convertView;
        }

        public class ViewHolder {
            ImageView logo;
            TextView mingcheng;
            TextView number;
            TextView time;
            TextView jiage;
        }

        /**
         * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
         */
        public int dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    }
}

